package com.hcinfos.gsoo.adapter;

import java.math.BigDecimal;
import java.util.List;

import android.content.Context;
import android.graphics.Bitmap;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.hcinfos.gsoo.GsooApplication;
import com.hcinfos.gsoo.R;
import com.hcinfos.gsoo.config.Constants;
import com.hcinfos.gsoo.enity.ShopInfo;
import com.hcinfos.gsoo.enity.ViewSuppSearchExt;
import com.hcinfos.gsoo.util.BitmapHelp;
import com.hcinfos.gsoo.util.ImageUtil;
import com.hcinfos.gsoo.util.ImageUtil.ImageCallback;
import com.lidroid.xutils.BitmapUtils;
import com.lidroid.xutils.bitmap.BitmapDisplayConfig;

public class ShopListAdapter extends BaseAdapter {

	private List<ViewSuppSearchExt> list;
	private LayoutInflater mInflater;
	public int index = 0;
	private Context context;
	private BitmapDisplayConfig bigPicDisplayConfig;
	private BitmapUtils bitmapUtils;

	public ShopListAdapter(Context context, List<ViewSuppSearchExt> list) {
		this.list = list;
		this.context = context;
		mInflater = LayoutInflater.from(context);
		bigPicDisplayConfig = new BitmapDisplayConfig();
		bigPicDisplayConfig.setLoadingDrawable(context.getResources()
				.getDrawable(R.drawable.tc_shop_default_logo));
		bigPicDisplayConfig.setLoadFailedDrawable(context.getResources()
				.getDrawable(R.drawable.tc_shop_default_logo));
		bitmapUtils = BitmapHelp.getBitmapUtils(context);
	}

	@Override
	public int getCount() {
		return list.size();
		// return 4;
	}

	@Override
	public Object getItem(int position) {
		return list.get(position);
	}

	@Override
	public long getItemId(int arg0) {
		return arg0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		final TypeHolder holder;
		if (convertView == null) {
			convertView = mInflater.inflate(R.layout.tc_shop_list_item, null);
			holder = new TypeHolder();
			holder.nameTv = (TextView) convertView
					.findViewById(R.id.shop_title);
			holder.icon_img = (ImageView) convertView
					.findViewById(R.id.shop_img);
			holder.price_tv = (TextView) convertView
					.findViewById(R.id.shop_price_num);
			holder.type_tv = (TextView) convertView
					.findViewById(R.id.shop_category_name);
			holder.distance_tv = (TextView) convertView
					.findViewById(R.id.shop_distance_data);
			holder.address_tv = (TextView) convertView
					.findViewById(R.id.shop_preferential);
			holder.shop_icon_quan = (ImageView) convertView
					.findViewById(R.id.shop_icon_quan);
			holder.shop_icon_zhe = (ImageView) convertView
					.findViewById(R.id.shop_icon_zhe);
			convertView.setTag(holder);
		} else {
			holder = (TypeHolder) convertView.getTag();
		}
		ViewSuppSearchExt searchExt = list.get(position);
		holder.nameTv.setText(searchExt.getSuppName());
		String consumpPer = searchExt.getConsumptionPer() == null ? "暂无"
				: ("￥" + searchExt.getConsumptionPer()) + "";
		holder.price_tv.setText(consumpPer);
		holder.type_tv.setText(searchExt.getTag());
		holder.address_tv.setText(searchExt.getAddr());
		double distance = searchExt.getDistance();

		if (distance < 1000) {
			BigDecimal dif = new BigDecimal(distance);
			double disStr = dif.setScale(2, BigDecimal.ROUND_HALF_UP)
					.doubleValue();
			holder.distance_tv.setText(disStr + "m");
		} else {
			double dis = distance / 1000d;
			BigDecimal df = new BigDecimal(dis);
			double disStr = df.setScale(2, BigDecimal.ROUND_HALF_UP)
					.doubleValue();
			holder.distance_tv.setText(disStr + " km");
		}
		if (isSameCity) {
			holder.distance_tv.setVisibility(View.VISIBLE);
		} else {
			holder.distance_tv.setVisibility(View.GONE);
		}

		Integer coupon = searchExt.getCouponNum();
		Integer vip = searchExt.getVipNum();
		if (coupon == null) {
			holder.shop_icon_quan.setVisibility(View.GONE);// 优惠券
		} else {
			holder.shop_icon_quan.setVisibility(View.VISIBLE);
		}
		if (vip == null) {
			holder.shop_icon_zhe.setVisibility(View.GONE);// 会员卡
		} else {
			holder.shop_icon_zhe.setVisibility(View.VISIBLE);
		}
		String url = Constants.SERVER + searchExt.getImgAddr()
				+ Constants.IMG_THUMB_DIR + searchExt.getImgId() + ".jpg";
		if (searchExt.getDataType() != null
				&& searchExt.getDataType().equals(ViewSuppSearchExt.TYPES[1])) {
			if (searchExt.getPhotos() != null
					&& searchExt.getPhotos().size() > 0) {
				String imageUrl = searchExt.getPhotos().get(0).getUrl();
				bitmapUtils.display(holder.icon_img, imageUrl,
						bigPicDisplayConfig);
			} else {
				holder.icon_img
						.setImageResource(R.drawable.tc_shop_default_logo);
			}

		} else {
			if (searchExt.getImgId() != null) {
				bitmapUtils.display(holder.icon_img, url, bigPicDisplayConfig);
			} else {
				holder.icon_img
						.setImageResource(R.drawable.tc_shop_default_logo);
			}
		}
		return convertView;
	}

	private final class TypeHolder {
		TextView nameTv;
		ImageView icon_img;
		TextView price_tv;
		TextView type_tv;
		TextView distance_tv;
		TextView address_tv;
		ImageView shop_icon_quan;
		ImageView shop_icon_zhe;
	}

	private boolean isSameCity = true;

	public void setIsSameCity(boolean isSameCity) {
		this.isSameCity = isSameCity;
	}

	public void setDataChange(List<ViewSuppSearchExt> list) {
		this.list = list;
		notifyDataSetChanged();
	}

}
